package com.example.projectdemo.rvrv;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.projectdemo.R;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/***
 * 双RV联动
 * 1.点击左边的：
 * 当点击时，将左侧的recyclerview点击的item设置为选中状态
 * 右边滑动到选中的位置
 * 2.滑动右边的
 * 当在右侧滑动时，因其显示内容实时在变化，所以需要根据当前屏幕最上方显示的内容来判断其属于左侧哪个item内的，以此来显示左侧item被选中状态。
 *
 */
public class RvRvActivity extends AppCompatActivity {

    private RecyclerView mLeft;
    private RecyclerView mRight;

    private List<String> mLeftList;
    private List<NavBean.DataBean> mRightList;
    private LeftAdapter mAdapter;
    private RightAdapter mAdapter1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rv_rv);

        mLeft = findViewById(R.id.rvLeft);
        mRight = findViewById(R.id.rvRight);

        mLeftList = new ArrayList<>();
        mRightList = new ArrayList<>();
        loadData();

        mAdapter = new LeftAdapter(this, mLeftList);
        mAdapter1 = new RightAdapter(this, mRightList);

        mLeft.setAdapter(mAdapter);
        mRight.setAdapter(mAdapter1);

        recyclerViewLinkage();

    }

    private void recyclerViewLinkage() {
        final LinearLayoutManager manager = (LinearLayoutManager) mRight.getLayoutManager();
        //左边联动右边
        mAdapter.setOnItemClickListener(new LeftAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                //view.setBackgroundResource(R.drawable.text_bg);
                mAdapter.setChoose(position);
                manager.scrollToPositionWithOffset(position, 0);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });

        //右边联动左边
        mRight.addOnScrollListener(new RecyclerView.OnScrollListener() {


            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //返回值就是右侧这个recyclerview在当前屏幕上第一个可见的item内容(并不需要全部可见)在这个适配器内的位置。
                int firstItemPosition = manager.findFirstVisibleItemPosition();
                //因为左右是一一对应的，所以当右侧第几个item可见时，意味着，它是属于左侧第几个item的内容的，
                // 这个时候就可以调用 rvLeft.smoothScrollToPosition(firstItemPosition)，来进行滚动，同时设置为选中状态。
                Log.i("zxd", "onScrolled: " + firstItemPosition);
                if (firstItemPosition != -1) {
                    mLeft.smoothScrollToPosition(firstItemPosition);
                    mAdapter.setChoose(firstItemPosition);
                }
                //如果滑倒底，设置左边选中最后一个
//                Log.i("zxd", "-1: " + recyclerView.canScrollVertically(-1));
//                Log.i("zxd", "1: " + recyclerView.canScrollVertically(1));
                //效果有点丑，不知道怎么美化
                if (recyclerView.canScrollVertically(-1) && !recyclerView.canScrollVertically(1)) {
                    mLeft.smoothScrollToPosition(mLeftList.size() - 1);
                    mAdapter.setChoose(mLeftList.size() - 1);
                }
            }
        });

    }

    private void loadData() {
        String url = "https://www.wanandroid.com/";
        //1、获取Retrofit对象
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                //增加返回值为Json的支持
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //2、然后通过动态代理获取到所定义的接口，获取到返回
        ApiService ipServiceForPath = retrofit.create(ApiService.class);
        Call<NavBean> call = ipServiceForPath.getNav();//1
        //3、异步请求
        //同步请求是call.execute()
        call.enqueue(new Callback<NavBean>() {
            @Override
            public void onResponse(Call<NavBean> call, Response<NavBean> response) {
                List<NavBean.DataBean> data = response.body().getData();
                for (int i = 0; i < data.size(); i++) {
                    mLeftList.add(data.get(i).getName());
                }
                mRightList.addAll(data);

                mAdapter.notifyDataSetChanged();
                mAdapter1.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Call<NavBean> call, Throwable t) {
                Toast.makeText(RvRvActivity.this, "失败", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
